﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Web;

namespace Portal.CMS
{
    public class AddVote : WebPart
    {
        int MaxItems = 10;
        public override string WebPartId
        {
            get
            {
                return "AddVote";
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Thêm mới trưng cầu ý kiến");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }


        public override void LoadSubControls(System.Web.UI.WebControls.PlaceHolder pl, System.Web.UI.Page Page, LoadParamCls LoadParamCls)
        {
            VoteCls VoteCls = null;
            if (HttpContext.Current.Request["VoteId"] != null)
            {
                string VoteId = (string)HttpContext.Current.Request["VoteId"];
                VoteCls = new VoteBLL().CreateModel(Environments.ActiveLanguage, VoteId);
            }
            
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CMS.AddVote));

            string SelectLanguageText =
              "<select id=divSelectLanguage>";
            string[] Languages = Environments.GetLanguages();
            for (int iIndex = 0; iIndex < Languages.Length; iIndex++)
            {
                if (Environments.ActiveLanguage.Equals(Languages[iIndex]))
                {
                    SelectLanguageText += "<option selected value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
                else
                {
                    SelectLanguageText += "<option value=\"" + Languages[iIndex] + "\">" + Languages[iIndex] + "</option>";
                }
            }
            SelectLanguageText += "</select>";

            string belongSite = (string)Portal.Environment.Environments.Request("belongSite");
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "Vote.js") +
                            // "<div class=cssForm>" +
                             //"<div class=cssFormTitle>" + ResourceBLL.GetText(WebPartId, "AddVoteFormTitle", "Thêm mới thăm dò ý kiến") + "</div>" +
                             "<input id=txtBelongSite type=hidden value=\""+belongSite+"\">"+
                             "<div class=cssFormContent>" +
                             "          <div>"+
                             "          <table>" +
                             "              <tr>" +
                             "                  <td>" + ResourceBLL.GetText(WebPartId, "AddVoteLanguage", "Ngôn ngữ") + "</td>" +
                             "                  <td>" + SelectLanguageText + "</td>" +
                             "              </tr>" +
                             "              <tr>" +
                             "                  <td>" + ResourceBLL.GetText(WebPartId, "AddVoteCaption", "Tiêu đề") + "</td>" +
                             "                  <td><input class=cssTextBoxDialog id=txtSubject> </td>" +
                             "              </tr>" +
                             "              <tr>"+
                             "                  <td></td>"+
                             "                  <td><input type=button value=\"Ghi lại\" onclick=javascript:ClientSaveAddVote();><input type=button value=\""+ResourceBLL.GetText(WebPartId,"CloseButton","Đóng")+"\" onclick=javascript:window.close();></td>"+
                             "          </table>" +
                             "      </div>" +
                             "      <div class=cssVoteItems>" +
//                             "          <div></div>" +//+ ResourceBLL.GetText(WebPartId, "RowItemTitle", "Chi tiết") + "<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "AppendRow", "Thêm chi tiết") + "\" onclick=javascript:AppendNewRow();>
                             "          <table>" +
                             "              <tr>" +
                             "                  <th class=cssItemName>" + ResourceBLL.GetText(WebPartId, "LinkItemName", "Tiêu đề") + "</th>" +
                             //"                  <th class=cssItemRemove>" + ResourceBLL.GetText(WebPartId, "RemoveText", "(xóa)") + "</th>" +
                             "              <tr>";
            for (int Index = 0; Index < MaxItems; Index++)
            {
                //RetHtml+=ServerSideNewLinkItem(iIndex);
                RetHtml+=
                          "<tr>" +
                          "     <td class=cssItemName><input class=cssTextBoxDialog name=\"txtItemName_" + Index + "\" id=\"txtItemName_" + Index + "\" class=cssInputItemName></td>\r\n" +
                          "</tr>";
            }
            RetHtml += "  </table>" +
                          "</div>"; //div content
                //"</div>";//div form

            RetHtml = Environment.Environments.GetFormDialog(RetHtml, ResourceBLL.GetText(WebPartId, "AddVoteFormTitle", "Thêm mới thăm dò ý kiến"));
            pl.Controls.Add(new System.Web.UI.LiteralControl(RetHtml));
        }
        
        //[AjaxPro.AjaxMethod]
        //public string ServerSideNewLinkItem(int Index)
        //{
        //    string Html = "<div class=cssDivRowItem id=divRowItem_" + Index + ">" +
        //                  "     <div id=divItemName_" + Index + " class=cssItemName><input name=\"txtItemName_" + Index + "\" id=\"txtItemName_" + Index + "\" runat=server/ class=cssInputItemName></div>\r\n" +
        //                  //"     <div id=divRemoveItem_" + Index + " class=cssRemoveItem><input class=cssButtonRemoveItem type=button value=\""+ResourceBLL.GetText(WebPartId,"RemoveText","(xóa)")+"\" onclick=javascript:RemoveItem(\""+Index+"\");></div>\r\n" +
        //                  "</div>";
        //    return Html;                        
        //}

        [AjaxPro.AjaxMethod]
        public string ServerSideAddVote(
            string belongSite,
            string ActiveLanguage,
            string VoteId,
            string VoteName, 
            VoteItemCls[] VoteItems)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                if (VoteName.Trim().Length == 0) throw new Exception(ResourceBLL.GetText(WebPartId,"EnterName","Nhập tiêu đề!"));
                if (VoteId == null)
                {
                    new VoteBLL().AddVote(null, belongSite,ActiveLanguage, VoteName, VoteItems);
                }
                else
                {
                    new VoteBLL().AppendVote(null,ActiveLanguage, VoteId, VoteName,VoteItems);
                }
                return ResourceBLL.GetText(WebPartId,"MessageAddOk","Tạo mới thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public VoteItemCls[] GetVoteItemsObject()
        {
            VoteItemCls[] VoteItems = new VoteItemCls[MaxItems];
            for (int iIndex = 0; iIndex < VoteItems.Length; iIndex++)
            {
                VoteItems[iIndex] = new VoteItemCls();
            }
            return VoteItems;
        }

        [AjaxPro.AjaxMethod]
        public string ProcessHtml(string Html,int Index)
        {
            Html=Html.Replace("<div class=\"cssDivRowItem\" id=\"divRowItem_"+Index+"\"></div>","");
            return Html;
        }
    }
}